package com.yiman.project.system.web;

import com.yiman.framework.web.BaseController;
import com.yiman.framework.web.domain.AjaxResult;
import com.yiman.framework.web.page.TableDataInfo;
import com.yiman.project.system.model.Role;
import com.yiman.project.system.service.MenuService;
import com.yiman.project.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: Mr.yang
 * @create: 2019-06-05
 **/
@Controller
public class RoleController extends BaseController {

    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;

    @RequestMapping("/system/role")
    public String rolePage(){
        return "system/role/role";
    }

    @RequestMapping("/addRolePage")
    public String addRolePage(ModelMap map){
        map.put("treeData",menuService.getLtreeMenus(null));
        return "system/role/add";
    }

    @RequestMapping("/getRoleList")
    @ResponseBody
    public TableDataInfo getRoleList(Role role){
        startPage();
        List<Role> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }

    /**
     * 修改保存角色
     */
    @RequestMapping("/system/role/editRole")
    @ResponseBody
    public AjaxResult editSave(Role role) {
        try {
            roleService.updateRole(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("服务异常");
        }
        return AjaxResult.success("修改成功");
    }

    @RequestMapping("/editRolePage/{roleId}")
    public String editRole(@PathVariable("roleId")Integer roleId, ModelMap map){
        map.put("role",roleService.selectByPrimaryKey(roleId));
        map.put("treeData",menuService.getLtreeMenus(roleId));
        return "system/role/edit";
    }

    /**
     * 新增保存角色
     */
    @RequestMapping("/system/role/addRole")
    @ResponseBody
    public AjaxResult addSave(Role role) {

        try {
            roleService.insertRole(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("服务异常");
        }
        return AjaxResult.success("添加成功");
    }

    @RequestMapping("/deleteRole")
    @ResponseBody
    public AjaxResult deleteRole(String ids){
        try {
            roleService.deleteRoleByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("服务异常");
        }
        return AjaxResult.success("删除成功");
    }
}
